package com.jet.merchant.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jet.core.enums.CommonDeleted;
import com.jet.core.enums.CommonStatus;
import com.jet.core.enums.IEnum;
import com.jet.core.exception.CustomException;
import com.jet.core.model.vo.PageVo;
import com.jet.core.utils.IdUtil;
import com.jet.merchant.domain.dto.CommissionRuleDto;
import com.jet.merchant.domain.entity.ClerkCommissionRule;
import com.jet.merchant.domain.query.CommissionRuleQuery;
import com.jet.merchant.domain.vo.CommissionRuleVo;
import com.jet.merchant.mapper.ClerkCommissionRuleMapper;
import com.jet.merchant.service.IClerkCommissionRuleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author author
 * @since 2025-02-25
 */
@Service
public class ClerkCommissionRuleServiceImpl extends ServiceImpl<ClerkCommissionRuleMapper, ClerkCommissionRule> implements IClerkCommissionRuleService {

    @Override
    public void create(long merchantNo, CommissionRuleDto dto) {
        ClerkCommissionRule entity = new ClerkCommissionRule();
        BeanUtils.copyProperties(dto, entity);
        entity.setId(IdUtil.generateLower());
        entity.setMerchantNo(merchantNo);
        super.save(entity);
    }

    @Override
    public void edit(String id, CommissionRuleDto dto) {
        ClerkCommissionRule entity = new ClerkCommissionRule();
        BeanUtils.copyProperties(dto, entity);
        entity.setId(id);
        super.updateById(entity);
    }

    @Override
    public void editStatus(String id, int status) {
        if (IEnum.getByCode(status, CommonStatus.class) == null) {
            throw new CustomException("状态信息错误");
        }
        ClerkCommissionRule entity = new ClerkCommissionRule();
        entity.setId(id);
        entity.setStatus(status);
        super.updateById(entity);
    }

    @Override
    public CommissionRuleVo findById(String id) {
        ClerkCommissionRule entity = super.getById(id);
        if (entity == null) {
            return null;
        }
        CommissionRuleVo vo = new CommissionRuleVo();
        BeanUtils.copyProperties(entity, vo);
        return vo;
    }

    @Override
    public List<CommissionRuleVo> list(long merchantNo, CommissionRuleQuery query) {
        Wrapper<ClerkCommissionRule> wrapper = new LambdaQueryWrapper<ClerkCommissionRule>()
                .eq(ClerkCommissionRule::getMerchantNo, merchantNo)
                .eq(query.getStatus() != null, ClerkCommissionRule::getStatus, query.getStatus())
                .orderByDesc(ClerkCommissionRule::getCreateTime);
        List<ClerkCommissionRule> list = super.list(wrapper);
        return list.stream().map(x -> {
            CommissionRuleVo vo = new CommissionRuleVo();
            BeanUtils.copyProperties(x, vo);
            return vo;
        }).collect(Collectors.toList());
    }

    @Override
    public PageVo<CommissionRuleVo> page(long merchantNo, CommissionRuleQuery query) {
        Page<ClerkCommissionRule> tempQuery = new Page<>(query.getPageIndex(), query.getPageSize());
        Wrapper<ClerkCommissionRule> wrapper = new LambdaQueryWrapper<ClerkCommissionRule>()
                .eq(ClerkCommissionRule::getMerchantNo, merchantNo)
                .eq(ClerkCommissionRule::getDeleted, CommonDeleted.NOT_DELETED.getCode())
                .orderByDesc(ClerkCommissionRule::getCreateTime);
        IPage<ClerkCommissionRule> page = super.page(tempQuery, wrapper);
        if (CollectionUtils.isEmpty(page.getRecords())) {
            return PageVo.result();
        }
        List<CommissionRuleVo> vos = page.getRecords().stream().map(x -> {
            CommissionRuleVo vo = new CommissionRuleVo();
            BeanUtils.copyProperties(x, vo);
            return vo;
        }).collect(Collectors.toList());
        return PageVo.result(page, vos);
    }
}
